import React from 'react';
import { Checkbox } from 'antd';
import Modal from '@/zero/components/Modal';
import Text from './_components/Text';

/**
 * 单选模式
 */
const Index = (props) => {
  const {
    option = {},

    value,
    onChange,
    disabled,

    changeConfirm,

    ...rest
  } = props;

  if (disabled && disabled.render) {
    return <Text value={value} option={option} render={disabled.render} />;
  }

  return (
    <>
      <Checkbox
        {...rest}
        disabled={!!disabled}
        checked={value === option.value}
        onChange={(event) => {
          const checkedValue = event.target.checked ? option.value : option.noMatch;

          const push = () => {
            if (onChange) {
              onChange(checkedValue);
            }
          };
          if (changeConfirm) {
            const { triggerValues } = changeConfirm;
            if (
              triggerValues === undefined ||
              (triggerValues !== undefined && triggerValues.includes(checkedValue))
            ) {
              Modal.confirm({
                title: 'Bla bla ...',
                content: '',
                ...changeConfirm,
                onOk: push,
              });
              return;
            }
          }
          push();
        }}
      >
        {option.label}
      </Checkbox>
    </>
  );
};

Index.displayName = 'SingleCheckbox';
export default Index;
